﻿using CuocChienQueNha.Class.Character;
using CuocChienQueNha.Class.Character.army;
using CuocChienQueNha.Class.MapClass;
using Microsoft.Phone.Controls;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Navigation;

namespace CuocChienQueNha
{
    public partial class GamePage : PhoneApplicationPage
    {
        ContentManager contentManager;
        GameTimer timer;
        SpriteBatch spriteBatch;
        Random rand;
        Assets tempAssets;
        List<Enemy> enemys;
        List<BasicArmy> armys;

        #region nơi đặt các biến static
        public static Vector2 sizeMap = new Vector2(12, 20);// kích thước ma trận
        public static Vector2 sizeImageBackGround = new Vector2(800, 480);// kích thước ảnh nền
        public static int level; //level của màn chơi
        public static bool isPause;// có tạm dừng hay không?
        public static Map curMap;
        public static int Health = 20;
        #endregion

        #region nơi đặt các biến
        SpriteFont font;
        #endregion

        public GamePage()
        {
            InitializeComponent();

            // Get the content manager from the application
            contentManager = (Application.Current as App).Content;

            // Create a timer for this page
            timer = new GameTimer();
            timer.UpdateInterval = TimeSpan.FromTicks(333333);
            timer.Update += OnUpdate;
            timer.Draw += OnDraw;
            armys = new List<BasicArmy>();
            rand = new Random();
            level = 1;
            CreateGame(level);
            //for (int i = 1; i < 10; i++)
            {
                CreateEnemy(rand.Next(1, 10000) % 15 + 1, curMap.getBeGin(), "Bottom");
            }
            armys.Add(new BasicArmy("shraman", 0));
            armys[0].setIndex(new Vector2(6, 8));
            armys.Add(new BasicArmy("shraman", 1));
            armys[1].setIndex(new Vector2(6, 12));
            armys.Add(new BasicArmy("shraman", 2));
            armys[2].setIndex(new Vector2(6, 14));
        }

        void CreateGame(int index)
        {
            level = index;
            isPause = false;
            curMap = new Map(level);
            enemys = new List<Enemy>();
        }

        void CreateEnemy(int Type, Vector2 index, string Huong)
        {
            tempAssets = new Assets(Huong, 6, 0, 100);
            enemys.Add(new Enemy(Type, index, tempAssets));
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            // Set the sharing mode of the graphics device to turn on XNA rendering
            SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(true);

            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(SharedGraphicsDeviceManager.Current.GraphicsDevice);

            // TODO: use this.content to load your game content here
            curMap.Load(contentManager);
            font = contentManager.Load<SpriteFont>("Fonts/miniFont");
            foreach (Enemy enemy in enemys)
            {
                enemy.Load(contentManager);
            }
            foreach (BasicArmy army in armys)
            {
                army.Load(contentManager);
            }
            // Start the timer
            timer.Start();

            base.OnNavigatedTo(e);
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            // Stop the timer
            timer.Stop();

            // Set the sharing mode of the graphics device to turn off XNA rendering
            SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(false);

            base.OnNavigatedFrom(e);
        }

        /// <summary>
        /// Allows the page to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        private void OnUpdate(object sender, GameTimerEventArgs e)
        {
            // TODO: Add your update logic here
            foreach (Enemy enemy in enemys)
            {
                if (enemy.DoiHuong()) enemy.Load(contentManager);
                enemy.Dichuyen();
            }
        }

        /// <summary>
        /// Allows the page to draw itself.
        /// </summary>
        private void OnDraw(object sender, GameTimerEventArgs e)
        {
            SharedGraphicsDeviceManager.Current.GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here
            spriteBatch.Begin();
            curMap.Draw(spriteBatch);
            foreach (Enemy enemy in enemys)
            {
                if (enemy.image == null)
                    enemy.Load(contentManager);
                enemy.Draw(spriteBatch);
            }
            foreach (BasicArmy army in armys)
            {
                if (army.image == null)
                    army.Load(contentManager);
                army.Draw(spriteBatch, font);
            }
            spriteBatch.End();
        }
    }
}